package com.ehaoyao.web;

import com.ehaoyao.entity.MonitorPersonExportVo;
import com.ehaoyao.entity.MonitorPersonImportVo;
import com.ehaoyao.entity.PersonExportVo;
import com.ehaoyao.utils.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: demo
 * @description: ExcelApi
 * @author: zhangyp
 * @create: 2020-08-13
 */
@Slf4j
@RestController
@RequestMapping("/excel")
public class ExcelApi {

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void exportExcel(HttpServletResponse response) throws IOException {
        long start = System.currentTimeMillis();
        List<MonitorPersonExportVo> personList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            MonitorPersonExportVo personVo = new MonitorPersonExportVo();
            personVo.setName("张三" + i);
            personVo.setUsername("张三" + i);
            personVo.setPhoneNumber("18888888888");
            personVo.setImageUrl("https://tpc.googlesyndication.com/simgad/12277888657510259338?sqp=4sqPyQQ7QjkqNxABHQAAtEIgASgBMAk4A0DwkwlYAWBfcAKAAQGIAQGdAQAAgD-oAQGwAYCt4gS4AV_FAS2ynT4&rs=AOga4ql_pdKy0SAo8zaFccX6af22J6siiQ");
            personList.add(personVo);
        }
        log.debug("导出excel所花时间：" + (System.currentTimeMillis() - start));
        ExcelUtils.exportExcel(personList, "员工信息表", "员工信息", MonitorPersonExportVo.class, "员工信息", response);
    }

    /**
     * 导入
     *
     * @param file
     */
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public String importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        long start = System.currentTimeMillis();
        List<MonitorPersonImportVo> personVoList = ExcelUtils.importExcel(file, MonitorPersonImportVo.class);
        log.info(personVoList.toString());
        log.info("导入excel所花时间：" + (System.currentTimeMillis() - start));
        return "导入成功";
    }

}